package com.sgb.kjwl.viewmodel.lookingforAJob;

import androidx.databinding.ObservableField;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.FindJobRequestEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.lookingforAJob.FindWorksActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 找活干
 */
public class FindWorksAtvModel extends BaseViewModel {

    public ObservableField<FindJobRequestEntity> filterJson = new ObservableField<FindJobRequestEntity>(new FindJobRequestEntity());
    private final FindWorksActivity activity;
    private HomeRepertory repertory;
    private PickSelectEntity all;
    private int page = 1;
    private int limit = 10;
    private Map map;

    public FindWorksAtvModel(FindWorksActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    // 列表
   /* public void getNewList( String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo, String fourCategoryNo,String fiveCategoryNo,String sixCategoryNo, String cityNo, boolean b) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("areacode", repertory.getAreaCode());
        map.put("limit", limit);
        map.put("page", page);

   *//*     //支付方式
        if (!"".equals(payfor) || payfor != null)
            map.put("payfor", payfor);*//*
        if (!"".equals(firstCategoryNo) && firstCategoryNo != null)
            map.put("firstCategoryNo", firstCategoryNo);
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null)
            map.put("secondCategoryNo", secondCategoryNo);
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null)
            map.put("thirdCategoryNo", thirdCategoryNo);
        if (!"".equals(cityNo) && cityNo != null)
            map.put("cityNo", cityNo);

        if (!"".equals(fourCategoryNo) && fourCategoryNo != null)
            map.put("fourCategoryNo", fourCategoryNo);

        if (!"".equals(fiveCategoryNo) && fiveCategoryNo != null)
            map.put("fifthCategoryNo", fiveCategoryNo);
        if(!"".equals(sixCategoryNo)&&sixCategoryNo!=null){
            map.put("sixthCategoryNo",sixCategoryNo);
        }

        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getFindJob(map).enqueue(new Callback<BaseEntity<List<FindJobEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindJobEntity>>> call, Response<BaseEntity<List<FindJobEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() >= limit) {
                        activity.showList(response.body().getData(), b, false);
                    } else {
                        activity.showList(response.body().getData(), b, true);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindJobEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }*/

    public void getBiddingList(String typeNo, String statusNo, String price, String search, String provinceNo, String cityNo, String goodsType, String sortMethod, boolean b) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        if (!"".equals(provinceNo) && provinceNo != null)
            map.put("areacode", provinceNo);
        if (!"".equals(cityNo) && cityNo != null)
            map.put("city", cityNo);
        map.put("limit", 10);
        map.put("name", search);
        map.put("page", page);
        map.put("state", statusNo);
        if (!"".equals(typeNo) && typeNo != null)
            map.put("type", typeNo);
        if (!"".equals(price) && price != null)
            map.put("price", price);
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsType", goodsType);
        }
        map.put("sortMethod", Integer.valueOf(sortMethod));
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getBiddingList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<BiddingEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<BiddingEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null)
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        activity.showList(data.getData().getList(), b, data.getData().isLastPage());
                    }
                }
            }
        });
    }

    //新增类型过滤接口
    public void getTenderGoodsCategoryAllList(String category) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("type", category);
        APIManager.getInstance().getPackerAPI().getTenderGoodsCategoryAllList(map).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCategoryPop(data.getData());
                }
            }
        });
    }

    public void getPickTimes() {
        APIManager.getInstance().getPickAPI().getPickTimes().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showTimePopWindow(data.getData());
                }
            }
        });
    }

    public void getPriceData() {
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showPricePopWindow(data.getData());
                }
            }
        });
    }
    public void getUrlSetting(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    activity.getUrl(url,id);
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    // -------------------------------- 省市区 --------------------------------

    public String getAreaCode() {
        return repertory.getAreaCode();
    }

    public String getAreaName() {
        return repertory.getAreaName();
    }

    public void getCityList() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            if (response.body().getData().get(i) == null) return;
                            if (TextUtils.equals(response.body().getData().get(i).getNo(), getAreaCode())) {
                                activity.showCityPop(response.body().getData());
                                return;
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
